* Figure 4 (right panel), Figure A.1, and Figure A.3 (right panels of parts (a) and (b)).

use "temp/SI_clean.dta", clear
egen sector_year = group(year sector)
local t=97
local t2 = `t' + 1
gen i`t' = 0
replace i`t'=1 if year==19`t'
foreach x of var mi si xi {
gen `x'`t'b = 0
replace `x'`t'b = . if i`t'==1 & `x'==.
replace `x'`t'b = `x' * i`t'
bys id: egen `x'`t' = total(`x'`t'b)
replace `x'`t'=. if `x'`t'b==. & `x'`t'==0
drop `x'`t'b
}
gen deltam = mi - mi`t'
gen growthm = deltam / mi`t' * 100
gen deltas = si - si`t'
gen deltae = xi- xi`t'
gen lmi = log(mi)
gen lmi`t' = log(mi`t')
gen deltamlog = lmi - lmi`t'
local graphtitle "Indonesia"

* Figure 4 (right panel)
binscatter growthm si`t' if year>=19`t2' & year<=2000& si`t'>0 & si`t'!=., absorb(sector_year) n(80) xtitle(Initial Import Share) ytitle(Market Share Growth) ///
	       title("{bf: `graphtitle'}", color(black)) graphregion(color(white)) xlabel(0 0.5 1)
graph export "../../Output/F4_B.pdf", replace

* Figure A3 (a) and (b) (right panels)
binscatter deltas deltae if year>=19`t2' & year<=2000 & deltae!=0 & deltae!=.& deltas!=0 & deltas!=., absorb(sector_year) n(80) xtitle(Change Export Share) ytitle(Change Import Share) ///
           title("{bf: `graphtitle'}", color(black)) graphregion(color(white))
graph export "../../Output/FA3_a_2.pdf", replace
binscatter deltamlog deltae if year>=19`t2' & year<=2000 & deltae!=0 & deltae!=.& deltamlog!=0 & deltamlog!=., absorb(sector_year) n(80) xtitle(Change Export Share) ytitle(Change Log Market Share) ///
	       title("{bf: `graphtitle'}", color(black)) graphregion(color(white))
graph export "../../Output/FA3_b_2.pdf", replace

* Figure A.1 (right panel)
binscatter xi si, absorb(sector_year) n(80) xtitle(Import Share) ytitle(Export Share) graphregion(color(white))  /// 
	       title("{bf: `graphtitle'}", color(black)) graphregion(color(white)) xlabel(0 0.5 1)
graph export "../../Output/FA1_B.pdf", replace
